package com.breakfast.fun.other;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.breakfast.fun.R;
import com.breakfast.fun.data.entity.home.Products;
import com.breakfast.fun.event.ScrollEvent;
import com.breakfast.fun.ui.home.gallery.GalleryProductActivity;
import com.breakfast.fun.util.ImageUtil;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by hp on 2016/8/21.
 */
public class ProductModel implements LayoutId {

    private Products products;

    public ProductModel(Products products) {
        this.products = products;
    }

    @Override
    public int getItemLayoutId() {
        return R.layout.item_home_product;
    }

    @Override
    public View getView(Context context) {
        View view = LayoutInflater.from(context).inflate(getItemLayoutId(), null);
        ImageView imageView = (ImageView) view.findViewById(R.id.iv_home_product);
        ImageUtil imageUtil = new ImageUtil(context);
        imageUtil.loadUrlImage(products.getProduct().getImage(), imageView);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String category = products.getCategoryName();
                if (category == null) {
                    EventBus.getDefault().post(new ScrollEvent());
                } else {
                    Intent intent = new Intent(context,
                            GalleryProductActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("products", products);
                    if (TextUtils.isEmpty(products.getProduct().getClosetime())) {
                        bundle.putString("closeTime", "22:00:00");
                    } else {
                        bundle.putString("closeTime", products.getProduct().getClosetime());
                    }
                    bundle.putBoolean("isFromGallery", true);
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                }
            }
        });
        return view;
    }
}
